package com.bingxue.edu.parent.service;

import com.bingxue.edu.common.enums.ClassStatusEnum;
import com.bingxue.edu.framework.common.model.PageQuery;
import com.bingxue.edu.framework.common.model.Pagination;
import com.bingxue.edu.management.education.service.ClassInfoService;
import com.bingxue.edu.management.education.service.EnrollPermissionService;
import com.bingxue.edu.management.shop.model.req.OrderCreateReq;
import com.bingxue.edu.management.shop.service.OrderService;
import com.bingxue.edu.parent.model.query.ParentShopClassQuery;
import com.bingxue.edu.parent.model.resp.StudentShopClassResp;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryWrapper;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;

import static com.bingxue.edu.management.education.model.entity.table.ClassInfoTableDef.CLASS_INFO;
import static com.bingxue.edu.management.system.model.entity.table.EmployeeTableDef.EMPLOYEE;
import static com.bingxue.edu.management.system.model.entity.table.OrgTableDef.ORG;

@Slf4j
@Service
@RequiredArgsConstructor
public class ParentShopService {

    private final ClassInfoService classInfoService;
    private final EnrollPermissionService enrollPermissionService;
    private final OrderService orderService;

    /**
     * 家长搜索课程，按权限过滤
     */
    public Pagination<StudentShopClassResp> getShopClassPage(ParentShopClassQuery query, PageQuery pageQuery) {
        QueryWrapper queryWrapper = buildQueryWrapper();
        queryWrapper.where(enrollPermissionService.buildPermissionFilterCondition(query.getStudentId(), query.getSubject()))
                .and(CLASS_INFO.NAME.like(query.getKeyword()).or(EMPLOYEE.NAME.like(query.getKeyword())))
                .and(CLASS_INFO.SUBJECT.eq(query.getSubject()))
                .and(CLASS_INFO.CLASS_YEAR.eq(query.getClassYear()))
                .and(CLASS_INFO.CLASS_CATEGORY.eq(query.getClassCategory()))
                .and(CLASS_INFO.CAMPUS_ID.eq(query.getCampusId()))
                .and(CLASS_INFO.TERM.eq(query.getTerm()))
                .and(CLASS_INFO.GRADE.eq(query.getGrade()))
                .and(CLASS_INFO.LEAD_TEACHER_ID.eq(query.getLeadTeacherId()))
                .and(CLASS_INFO.SALE_STATUS.eq(true))
                .and(CLASS_INFO.CLASS_STATUS.in(ClassStatusEnum.WAITING, ClassStatusEnum.TEACHING))
                .and(CLASS_INFO.ARRANGE_TEMPLATE_ID.eq(query.getArrangeTemplateId()))
                .and(CLASS_INFO.TIME_TEMPLATE_ID.eq(query.getTimeTemplateId()));
        Page<StudentShopClassResp> page = classInfoService.getMapper().paginateWithRelationsAs(pageQuery.toPage(), queryWrapper, StudentShopClassResp.class);
        return Pagination.of(page);
    }


    public List<StudentShopClassResp> getShopClassList(ParentShopClassQuery query) {
        QueryWrapper queryWrapper = buildQueryWrapper();
        queryWrapper.where(enrollPermissionService.buildPermissionFilterCondition(query.getStudentId(), query.getSubject()))
                .and(CLASS_INFO.NAME.like(query.getKeyword()).or(EMPLOYEE.NAME.like(query.getKeyword())))
                .and(CLASS_INFO.SUBJECT.eq(query.getSubject()))
                .and(CLASS_INFO.CLASS_YEAR.eq(query.getClassYear()))
                .and(CLASS_INFO.CLASS_CATEGORY.eq(query.getClassCategory()))
                .and(CLASS_INFO.CAMPUS_ID.eq(query.getCampusId()))
                .and(CLASS_INFO.TERM.eq(query.getTerm()))
                .and(CLASS_INFO.GRADE.eq(query.getGrade()))
                .and(CLASS_INFO.LEAD_TEACHER_ID.eq(query.getLeadTeacherId()))
                .and(CLASS_INFO.SALE_STATUS.eq(true))
                .and(CLASS_INFO.CLASS_STATUS.in(ClassStatusEnum.WAITING, ClassStatusEnum.TEACHING))
                .and(CLASS_INFO.ARRANGE_TEMPLATE_ID.eq(query.getArrangeTemplateId()))
                .and(CLASS_INFO.TIME_TEMPLATE_ID.eq(query.getTimeTemplateId()));
        return classInfoService.getMapper().selectListWithRelationsByQueryAs(queryWrapper, StudentShopClassResp.class);
    }


    public StudentShopClassResp getShopClassById(String classId) {
        QueryWrapper queryWrapper = buildQueryWrapper().where(CLASS_INFO.ID.eq(classId));
        return classInfoService.getMapper().selectOneWithRelationsByQueryAs(queryWrapper, StudentShopClassResp.class);
    }

    private QueryWrapper buildQueryWrapper() {
        return QueryWrapper.create()
                .select(
                        CLASS_INFO.DEFAULT_COLUMNS,
                        EMPLOYEE.NAME.as(StudentShopClassResp::getLeadTeacherName),
                        ORG.NAME.as(StudentShopClassResp::getCampusName)
                )
                .from(CLASS_INFO)
                .leftJoin(ORG).on(CLASS_INFO.CAMPUS_ID.eq(ORG.ID))
                .leftJoin(EMPLOYEE).on(CLASS_INFO.LEAD_TEACHER_ID.eq(EMPLOYEE.ID));
    }


    public String createOrder(OrderCreateReq req) {
        return orderService.createOrder(req);
    }
}
